package com.wzy.service.reposirory.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wzy.common.base.redis.RedisUtil;
import com.wzy.common.base.result.R;
import com.wzy.common.base.result.ResultCodeEnum;
import com.wzy.common.base.util.JacksonUtil;
import com.wzy.service.reposirory.dto.EveryDayWord;
import com.wzy.service.reposirory.dto.UserGlobalWord;
import com.wzy.service.reposirory.dto.WordsResponse;
import com.wzy.service.reposirory.entity.GlobalWord;
import com.wzy.service.reposirory.entity.UserWord;
import com.wzy.service.reposirory.feign.ScheduleService;
import com.wzy.service.reposirory.service.GlobalWordService;
import com.wzy.service.reposirory.service.UserSentenceService;
import com.wzy.service.reposirory.service.UserWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuzhengyu
 * @since 2021-03-13
 */
@RestController
@RequestMapping("/user-word")
//@CrossOrigin(origins = "*")
public class UserWordController {
    @Autowired
    UserWordService userWordService;

    @Autowired
    UserSentenceService userSentenceService;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    private ScheduleService scheduleService;

    @Autowired
    private GlobalWordService globalWordService;


    private static final String REDIS_KEY = "every-day-word-";

    /**
     * 查看用户单词库是否存在单词
     *
     * @param uid
     * @return
     */
    @RequestMapping(path = "uid", method = RequestMethod.GET)
    public R getUserWords(@RequestParam String uid) {
        return userWordService.getUserWords(uid);
    }


    @PutMapping("status")
    public R updateStatusById(@RequestParam String id, @RequestParam String status) {
        boolean update = userWordService.update(Wrappers.<UserWord>lambdaUpdate().eq(UserWord::getId, id).set(UserWord::getStatus, status));
        if (update) {
            return R.ok();
        } else {
            return R.error().message("更新用户单词状态失败");
        }
    }

    @PutMapping("status-sid")
    public R updateStatusBySId(@RequestParam String sid, @RequestParam String uid, @RequestParam String status) {
        userWordService.update(Wrappers.<UserWord>lambdaUpdate()
                .eq(UserWord::getUid, uid)
                .eq(UserWord::getSid, sid)
                .set(UserWord::getStatus, status));
        return R.ok();
    }


    @GetMapping("sentence-words")
    public R getUserWordBySid(String sid, String uid) {
        List<UserGlobalWord> userGlobalWordList = userWordService.getUserGlobalWordBySid(sid, uid);
        return R.ok().data("userWord", userGlobalWordList);
    }

    // 用户文章分解

    /**
     * 用户传入文章,对文章进行分解
     * //* @param article 文章
     * //* @param uid 用户id
     *
     * @return 将打碎的单词和句子返回
     */
    @RequestMapping(path = "break", method = RequestMethod.POST)
    public R articleBreak(@RequestBody Map<String, String> map) {
        String article = map.get("article");
        String uid = map.get("uid");
        if (article == null) {
            return R.error(ResultCodeEnum.PARAM_ERROR);
        }
        return userWordService.articleBreak(article, Long.valueOf(uid));
    }


    /**
     * 获取指定用户所有的指定状态单词
     *
     * @param uid    用户id
     * @param status 状态
     * @return
     */
    @RequestMapping(path = "word", method = RequestMethod.GET)
    public R getWord(@RequestParam String uid,
                     @RequestParam String status) {
        List<UserWord> userWords = userWordService.list(Wrappers.<UserWord>lambdaQuery().eq(UserWord::getUid, uid).eq(UserWord::getStatus, status));
        return R.ok().data("user_word", userWords);
    }

    /**
     * 获取指定用户所有单词
     *
     * @param uid 用户id
     * @return
     */
    @RequestMapping(path = "all-word", method = RequestMethod.GET)
    public R getWord(@RequestParam String uid) {
        List<UserWord> userWords = userWordService.list(Wrappers.<UserWord>lambdaQuery().eq(UserWord::getUid, uid));
        return R.ok().data("user_word", userWords);
    }

    /**
     * 根据id删除单词
     *
     * @param id user word表中单词的id
     * @return
     */
    @RequestMapping(path = "word", method = RequestMethod.DELETE)
    public R deleteWord(@RequestParam String id) {
        if (userWordService.removeById(id)) {
            return R.ok();
        } else {
            return R.error();
        }
    }


    /**
     * 修改redis中的状态
     *
     * @param everyDayWords
     * @return
     */
    @PutMapping("/every-day-word")
    public R redisEveryDayUpdate(@RequestBody Map<String, ArrayList<EveryDayWord>> everyDayWords, @RequestParam String uid) {

        String str = JacksonUtil.clazz2String(everyDayWords.get("everyDayWords"));

        return userWordService.wordNumberUpdate(str, uid);
    }

    @PutMapping("word-pass")
    public R wordUpdate(@RequestParam String id, @RequestParam String uid, @RequestParam boolean isPass) {
        return userWordService.wordUpdate(id, uid, isPass);
    }

    @GetMapping("words-state")
    public R getUserWordsByState(@RequestParam String uid, @RequestParam String status) {
        List<UserWord> wordList = userWordService.list(Wrappers.<UserWord>lambdaQuery()
                .eq(UserWord::getUid, uid)
                .eq(UserWord::getStatus, status));
        List<WordsResponse> res = new ArrayList<>();
        wordList.forEach(u -> {
            GlobalWord one = globalWordService.getOne(Wrappers.<GlobalWord>lambdaQuery().eq(GlobalWord::getId, u.getGwid()));
            res.add(new WordsResponse( u, one));
        });
        return R.ok().data("wordList", res);
    }

}


